package cn.afactory.tygz.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.MoneyShop;
import cn.afactory.tygz.model.MoneyShopList;
import cn.afactory.tygz.model.UserInfo;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.MainActivity;
import cn.afactory.tygz.ui.activities.ShopExchangeActivity;
import cn.afactory.tygz.ui.activities.TopicAddActivity;
import cn.afactory.tygz.ui.adapter.MoneyShopAdapter;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.utils.LocalUtils;


public class MoneyShopFragment extends BaseFragment {



    private GridView gridView;
    private MoneyShopAdapter adapter;
    private List<MoneyShop> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fg_money_shop, null);
        initView(view);
        showTitleBar(false);
        initData();
        return view;
    }

    private void initView(View view){

        gridView = (GridView) view.findViewById(R.id.gridView);
        list = new ArrayList<MoneyShop>();
        adapter = new MoneyShopAdapter(getActivity(),list);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                MoneyShop shop = list.get(position);
                Intent intent = new Intent(getContext(), ShopExchangeActivity.class);
                intent.putExtra("shop",shop);
                getActivity().startActivity(intent);


            }
        });
    }




    private void initData(){
        //showLoadingDialog(getString(R.string.dlg_msg_logining));
        NetManager.getInstance(getActivity()).doGetMoneyShop(new OnResponseListener<MoneyShopList>() {
            @Override
            public void onComplete(boolean isOK, MoneyShopList result, int errorCode, String errorMsg) {
                if (isOK && result != null) {
                    list = result.value;
                    adapter.setDataChange(result.value);
                } else {
                    LocalUtils.showToast(getActivity(), errorMsg);
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                LocalUtils.showToast(getActivity(), errorMessage);

            }
        });

    }




}
